<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>成绩打印</el-breadcrumb-item>
    </el-breadcrumb>
    <hr/>
    <div class="app-container" v-loading="loading">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true"
               label-width="68px">
        <el-form-item label="学校名称" prop="schoolName">
          <el-input
              v-model="queryParams.schoolName"
              placeholder="请输入学校名称"
              clearable
          />
        </el-form-item>
        <el-form-item label="准考证号" prop="studentCode">
          <el-input
              v-model="queryParams.studentCode"
              placeholder="请输入准考证号"
              clearable
          />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input
              v-model="queryParams.idCard"
              placeholder="请输入身份证号"
              clearable
          />
        </el-form-item>
        <el-form-item label="项目" prop="itemName">
          <el-select v-model="queryParams.itemName" clearable>
            <el-option label="跳绳" value="跳绳"/>
            <el-option label="排球" value="排球"/>
          </el-select>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-select v-model="queryParams.gender" clearable>
            <el-option label="男" value="0"/>
            <el-option label="女" value="1"/>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="gender">
          <el-select v-model="queryParams.status" clearable>
            <el-option label="未考" value="0"/>
            <el-option label="未上传" value="1"/>
            <el-option label="已上传" value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item label="组别" prop="groupNo">
          <el-input
              v-model="queryParams.groupNo"
              placeholder="请输入组别"
              clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          <el-button type="danger" icon="el-icon-refresh" size="mini" @click="deleteData">清空本地考生分组信息
          </el-button>
          <el-button type="success" icon="el-icon-refresh" size="mini" @click="getAll">全量获取【第三方】考生分组信息
          </el-button>
          <el-button type="primary" icon="el-icon-refresh" size="mini" @click="syncCloudResultLocalBy"
                     :disabled="multiple">同步云平台成绩至本地
          </el-button>
          <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handPrint">打印
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table v-loading="loading" border :data="studentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" width="55" align="center" prop="id"/>
      <el-table-column label="学校名称" align="center" prop="schoolName"/>
      <el-table-column label="项目代码" align="center" prop="itemCode"/>
      <el-table-column label="项目名称" align="center" prop="itemName"/>
      <el-table-column label="准考证号" align="center" prop="studentCode"/>
      <el-table-column label="身份证号" align="center" prop="idCard"/>
      <el-table-column label="姓名" align="center" prop="studentName"/>
      <el-table-column label="性别" align="center" prop="gender">
        <template slot-scope="scope">
          <div v-if="scope.row.gender===0">男</div>
          <div v-if="scope.row.gender===1">女</div>
        </template>
      </el-table-column>
      <el-table-column label="组号" align="center" prop="groupNo"/>
      <el-table-column label="组内序号" align="center" prop="trackNo"/>
      <el-table-column label="班级名称" align="center" prop="className"/>
      <el-table-column label="测试时间" align="center" prop="gmtStarted"/>
      <el-table-column label="成绩" align="center" prop="result"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <div v-if="scope.row.status==='0'">未考</div>
          <div v-if="scope.row.status==='1'">未上传</div>
          <div v-if="scope.row.status==='2'">已上传</div>
        </template>
      </el-table-column>
    </el-table>
    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
        :pageSizes=[30,50,100,150,200,250,300]
        :limit=50
        style="float: right"
    />
  </div>
</template>

<script>
import {studentsList, deleteAllStudents, getApiStudentsList, syncCloudResultLocal} from "@/api/students";
import Pagination from "@/components/Pagination/index.vue";

export default {
  name: 'Home',
  components: {Pagination},
  data() {
    return {
      loading: true,
      queryParams: {
        schoolName: null,
        studentCode: null,
        idCard: null,
        itemName: null,
        gender: null,
        groupNo: null,
        status: '',
        pageNum: 1,
        pageSize: 30
      },
      studentList: [],
      // 总条数
      total: 0,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      studentCodes: [],
      firstItemName: null
    };
  },
  metaInfo() {
    return {
      title: process.env.VUE_APP_TITLE,
      titleTemplate: '成绩打印'
    }
  },
  created() {
    this.getList();
  },
  mounted() {
  },
  methods: {
    // 查询
    handleQuery() {
      this.queryParams.pageNum = 1
      this.studentCodes = [];
      this.firstItemName = null;
      this.single = true;
      this.multiple = true;
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.studentCodes = [];
      this.firstItemName = null;
      this.single = true;
      this.multiple = true;
      this.queryParams = {
        schoolName: null,
        studentCode: null,
        idCard: null,
        itemName: null,
        gender: null,
        groupNo: null,
        status: '',
        pageNum: 1,
        pageSize: 30
      }
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 清空数据
    deleteData() {
      this.loading = true
      this.$confirm('确定清空【本地考生数据】吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteAllStudents().then(response => {
          this.$modal.msgSuccess("清空成功");
          this.handleQuery();
        })
      }).catch(() => {
      }).finally(() => {
            this.loading = false
          }
      )
    },
    // 获取数据
    getAll() {
      this.loading = true
      this.$confirm('确定获取全量数据吗？获取全量数据会先清空【本地考生数据】！！！', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        getApiStudentsList().then(response => {
          console.log(response)
          this.loading = false
          this.$modal.msgSuccess("获取成功");
          this.handleQuery();
        })
      }).catch(() => {
      }).finally(() => {

          }
      )
    },
    // 打印
    handPrint() {
      if (!this.queryParams.groupNo) {
        this.$message.warning('请输入组别！');
        return
      }
      // console.log(this.studentCodes)
      // 新窗口打开
      // 新窗口打开并传递参数
      const route = this.$router.resolve({
        path: '/group',
        query: {
          // studentCodes: JSON.stringify(this.studentCodes), // 将数组转为字符串
          groupNo: this.queryParams.groupNo
        }
      })
      window.open(route.href, '_blank', 'noopener,noreferrer')
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.studentCodes = selection.map(item => item.studentCode)
      // 检查所有 itemName 是否相同
      const firstItemName = selection[0].itemName;
      this.firstItemName = firstItemName
      const allSame = selection.every(item => item.itemName === firstItemName);
      if (!allSame) {
        this.$message.error('所选项目的名称必须相同！');
        this.studentCodes = [];
        this.firstItemName = null;
        this.single = true;
        this.multiple = true;
      } else {
        this.single = selection.length !== 1
        this.multiple = !selection.length
      }
    },
    getList() {
      this.loading = true
      studentsList(this.queryParams).then(response => {
        console.log(response)
        console.log(response.data)
        this.studentList = response.data
        this.total = response.total
        console.log(this.studentList)
      }).finally(() => {
            this.loading = false
          }
      )
    },
    // 同步选择的考生项目成绩
    syncCloudResultLocalBy() {
      this.loading = true
      this.$confirm('确定同步？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        try {
          const data = {
            zkhs: this.studentCodes,
            xcxm: this.firstItemName,
            pageNum: 1,
            pageSize: 100
          }
          syncCloudResultLocal(data).then(response => {
            if (200 === response.code) {
              this.$message.success(response.msg);
            } else {
              this.$message.warning(response.msg);
            }
            this.handleQuery()
          }).finally(() => {
                this.loading = false
              }
          )
        } catch (error) {
          console.log('验证失败', error);
        }
      }).catch(() => {
      }).finally(() => {

          }
      )

    }
  }
};
</script>

<style scoped>
.app-container {
  padding: 5px;
}
</style>
